enemyimg = Image.load("enemies/pictures/enemy.png", VRAM)

dofile("enemies/monsters.lua")

-- -- -- -- -- -- -- -- -- -- -- --
-- -- ENEMIES -- -- -- -- -- -- -- -
-- -- -- -- -- -- -- -- -- -- -- --

enemy ={}
enemy.x = {}
enemy.y = {}
enemy.dir = {}
enemy.speed = {}
enemy.wid = {}
enemy.hig = {}
enemy.hp = {}
enemy.max = 0
enemy.exp = {}
enemy.searchrange = {}
enemy.hunting = {} -- signals if the curent monster is hunting the hero
enemy.dmg = {}
enemy.attackrange = {}
enemy.attackrate = {}
enemy.attacklimit = {}
enemy.picture = {}



enemy.oht = {}
enemy.oht.x = {}
enemy.oht.y = {}
enemy.oht.text={}
enemy.oht.starttime = {}
enemy.oht.status = {}


-- -- -- -- -- -- -- --
-- -- MAINLOOP -- -- --
-- -- -- -- -- -- -- --

function enemy.run()
	for _i = 1, enemy.max do
		enemy.search(_i)
		if 		enemy.hunting[_i] == 0 then
	 			enemy.move(_i)
		elseif	enemy.hunting[_i] == 1 then enemy.hunt(_i)
		end	
		
		--prints the picture for the enemy
		screen.blit(SCREEN_DOWN,enemy.x[_i],enemy.y[_i], enemy.picture[_i])
		
		--oht for enemy
		if enemy.oht.status[_i] == 1 then enemy.oht.run(_i) end
		
	end
end

-- -- -- -- -- -- -- --
-- -- OHT ENEMY -- -- --
-- -- -- -- -- -- -- --

function enemy.oht.new(text,enemy_nr) 
	enemy.oht.status[enemy_nr] = 1
	enemy.oht.text[enemy_nr] = text
	enemy.oht.starttime[enemy_nr] = clock.second
	
end


function enemy.oht.run(enemy_nr)
	enemy.oht.x[enemy_nr] = enemy.x[enemy_nr]
	enemy.oht.y[enemy_nr] = enemy.y[enemy_nr] - 10
	if enemy.oht.y[enemy_nr] < 0 then enemy.oht.y[enemy_nr] = 0 end
	if enemy.oht.x[enemy_nr] < 0 then enemy.oht.x[enemy_nr] = 0 end
	if  clock.second - enemy.oht.starttime[enemy_nr] < 3
	then screen.print(SCREEN_DOWN,enemy.oht.x[enemy_nr],enemy.oht.y[enemy_nr],enemy.oht.text[enemy_nr])
	else enemy.oht.status[enemy_nr] = 0
	end
end




					
function enemy.new(hp,exp,searchrange,wid,hig,speed,dmg,range,rate,x,y,picture) -- function 
	enemy.max = enemy.max + 1
	
	enemy.picture[enemy.max] = picture
	enemy.hp[enemy.max] = hp
	enemy.dir[enemy.max] = math.random(0,8)
	enemy.hunting[enemy.max] = 0
	enemy.exp[enemy.max] = exp
	enemy.searchrange[enemy.max] = searchrange
	enemy.wid[enemy.max] = wid
	enemy.hig[enemy.max] = hig
	enemy.speed[enemy.max] = speed
	enemy.dmg[enemy.max] = dmg
	enemy.attackrange[enemy.max] = range
	enemy.x[enemy.max] = x
	enemy.y[enemy.max] = y
	enemy.attackrate[enemy.max] = rate
	enemy.attacklimit[enemy.max] = 0
end	




function enemy.move(i) -- handels the movement of the enemies
	
	 -- defines the movement direction if the monster doesn't hunt the Hero
	 	if enemy.hunting[i] == 0 then
			if math.random(1,30) == 1 then enemy.dir[i] = math.random(0,8) end
		end 
	-- calculates the new X|Y coordinates
		if enemy.dir[i] == 1 then enemy.y[i] = enemy.y[i] - enemy.speed[i]
		elseif enemy.dir[i] == 2 then 	enemy.x[i] = enemy.x[i]+ enemy.speed[i]
		elseif enemy.dir[i] == 3 then 	enemy.y[i] = enemy.y[i]+ enemy.speed[i]
		elseif enemy.dir[i] == 4 then 	enemy.x[i] = enemy.x[i]- enemy.speed[i]
		elseif enemy.dir[i] == 5 then 	enemy.x[i] = enemy.x[i]+ enemy.speed[i]
										enemy.y[i] = enemy.y[i]- enemy.speed[i]
		elseif enemy.dir[i] == 6 then 	enemy.x[i] = enemy.x[i]+ enemy.speed[i]
										enemy.y[i] = enemy.y[i]+ enemy.speed[i]
		elseif enemy.dir[i] == 7 then 	enemy.x[i] = enemy.x[i]- enemy.speed[i]
										enemy.y[i] = enemy.y[i]+ enemy.speed[i]
		elseif enemy.dir[i] == 8 then 	enemy.x[i] = enemy.x[i]- enemy.speed[i]
										enemy.y[i] = enemy.y[i]- enemy.speed[i]
		end
	-- avoids that an enemy can leave the screen
		if enemy.x[i] < 0 then enemy.x[i] = 0 end
		if enemy.x[i] + enemy.wid[i] > SCREEN_WIDTH then enemy.x[i] = SCREEN_WIDTH - enemy.wid[i] end
		if enemy.y[i] < 0 then enemy.y[i] = 0 end
		if enemy.y[i] + enemy.hig[i] > SCREEN_HEIGHT then enemy.y[i] = SCREEN_HEIGHT - enemy.hig[i] end
	
end


function enemy.search(i) -- checks if the monster is to hunt the hero
		if compare( (enemy.x[i] -enemy.searchrange[i]), (enemy.y[i] -enemy.searchrange[i]), (enemy.searchrange[i] * 2 + enemy.wid[i]) , (enemy.searchrange[i] * 2 + enemy.hig[i]), hero.x,hero.y, hero.wid, hero.hig) == true
		then 	enemy.hunting[i] = 1  
		else 	enemy.hunting[i] = 0
		end
	
end


function enemy.hunt(i)
	-- this functions chcks if the monster is close enough to hit the hero. If yes it attacks the hero. If not the enemy keeps persuading the hero.
	if compare( (enemy.x[i] -enemy.attackrange[i]), (enemy.y[i] -enemy.attackrange[i]), (enemy.wid[i]  + enemy.attackrange[i]) , (enemy.attackrange[i] + enemy.hig[i]), hero.x,hero.y, hero.wid, hero.hig) == true then enemy.hit(i)
		else
			if 		enemy.x[i] == hero.x and
				enemy.y[i] > hero.y then enemy.dir[i] = 1
			elseif 	enemy.x[i] < hero.x and
				enemy.y[i] == hero.y then enemy.dir[i] = 2
			elseif 	enemy.x[i] == hero.x and
				enemy.y[i] < hero.y then enemy.dir[i] = 3
			elseif 	enemy.x[i] > hero.x and
				enemy.y[i] == hero.y then enemy.dir[i] = 4
			elseif 	enemy.x[i] < hero.x and
				enemy.y[i] > hero.y then enemy.dir[i] = 5
			elseif 	enemy.x[i] < hero.x and
				enemy.y[i] < hero.y then enemy.dir[i] = 6
			elseif 	enemy.x[i] > hero.x and
				enemy.y[i] < hero.y then enemy.dir[i] = 7
			elseif 	enemy.x[i] > hero.x and
				enemy.y[i] > hero.y then enemy.dir[i] = 8
		end		
		enemy.move(i)
  end

end

function enemy.hit(i)
	if enemy.attacklimit[i] < clock.second then
		enemy.attacklimit[i] = clock.second + 1 * enemy.attackrate[i]
		if enemy.attackrate[i] > 59 then enemy.attackrate[i] = enemy.attackrate[i] - 60 end
		hero.hp = hero.hp - 2
	end
end





function enemy.damage(dmg,i) -- this function is tho deal damage to a monster.
	enemy.hp[i] = enemy.hp[i] - dmg
	enemy.oht.new(dmg, i)
	if enemy.hp[i] < 1 then enemy.destroy(i) end
end




function enemy.destroy(ekill) --if monster dies, this functions delet the monster and gives  exp to the hero
	hero.exp = hero.exp + enemy.exp[ekill]
	table.remove(enemy.oht.status, ekill)
	table.remove(enemy.x, ekill)
	table.remove(enemy.y, ekill)
	table.remove(enemy.speed, ekill)
	table.remove(enemy.wid, ekill)
	table.remove(enemy.hig, ekill)
	table.remove(enemy.exp, ekill)
	table.remove(enemy.searchrange, ekill)
	table.remove(enemy.hunting, ekill)
	table.remove(enemy.dmg, ekill)
	table.remove(enemy.attackrange, ekill)
	table.remove(enemy.attackrate, ekill)
	table.remove(enemy.attacklimit, ekill)
	table.remove(enemy.dmg, ekill)
	table.remove(enemy.hp, ekill)
	table.remove(enemy.oht.x, ekill)
	table.remove(enemy.oht.y, ekill)
	table.remove(enemy.oht.text, ekill)
	table.remove(enemy.oht.starttime, ekill)
	table.remove(enemy.picture, ekill)
	enemy.max = enemy.max - 1
end
